import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter } from '@angular/core';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { Http } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';

import { Service } from '../../../server/server';
import { Pagination } from '../../../public/serve/pcrudInfo';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
declare var $: any;

@Component({
  selector: 'app-journal-table',
  templateUrl: './journal-table.component.html'
})
export class JournalTableComponent implements OnInit {

  @Input() tableNumber: number;

  right: any = { "system": {}, "journal": {} };
  constructor(private server: Service, private router: Router, private tableListService: CudTableService, private http: HttpClient) {
    this.right = $.extend(this.right, server.getRight());
  }

  ngOnInit() {
    this.getTabList(1, 20, '', '', '', '', 3);

    //接受查询结果
    this.journalsearcheventEmit = this.server.journalsearcheventEmit.subscribe((value: any) => {
      this.tabVal = value;
    });
    //删除全部
    this.journaldeletealleventEmit = this.server.journaldeletealleventEmit.subscribe((value: any) => {
      if (value) {
        this.getTabList(1, 20, '', '', '', '', 3);
      }
    });
    //删除3个月
    this.journaldeletethreeentEmit = this.server.journaldeletethreeentEmit.subscribe((value: any) => {
      if (value) {
        this.getTabList(1, 20, '', '', '', '', 3);
      }
    });
  }

  //取消订阅
  public journalsearcheventEmit: any;
  public journaldeletealleventEmit: any;
  public journaldeletethreeentEmit: any;
  ngOnDestroy() {
    this.journalsearcheventEmit ? this.journalsearcheventEmit.unsubscribe() : '';
    this.journaldeletealleventEmit ? this.journaldeletealleventEmit.unsubscribe() : '';
    this.journaldeletethreeentEmit ? this.journaldeletethreeentEmit.unsubscribe() : '';
  }

  public tabVal: any;

  @Output()
  tabeListOutput: EventEmitter<string> = new EventEmitter();
  // 表格
  getTabList(pageNumber, pageSize, startLogTime, endLogTime, level, queryCriteria, queryType) {
    let url = this.server.baseUrl + 'admin/logs/list';
    let data = {
      pageNo: pageNumber,
      pageSize: pageSize,
      endLogTime: endLogTime,
      startLogTime: startLogTime,
      level: level,
      queryCriteria: queryCriteria,
      queryType: queryType,
    }
    //console.log(data)
    this.tableListService.postRequest(url, data).then((data: any) => {

      this.tabVal = data.result;
      this.tabeListOutput.emit(data.length);
      //console.log(data.result)
    }).catch();
  }

  //详情
  details(val) {
    let url = this.server.baseUrl + 'admin/logs/info';
    let data = { id: val.id };
    this.http.post(url, data).subscribe((res: any) => {
      this.server.journaldetailseventEmit.emit(res.result)
    })
  }


}
